﻿	
namespace tcom.tools
{
	using UnityEngine;
	using System.Collections;
	using System.Collections.Generic;
	using System;
	using System.IO;
	
	public class TEngine
	{
		
		public static string GetFilePath(string dataname)
		{
			string vfilename = "";
			
			if(Application.platform == RuntimePlatform.WindowsWebPlayer || 
				Application.platform == RuntimePlatform.OSXWebPlayer)
			{
				string pt = Application.absoluteURL;
				vfilename = pt.Substring(0, pt.Length - 17) + dataname;
			}
			else
			{
				vfilename = "file:///" + Application.dataPath + "/../" + dataname;
			}
			
			if(!vfilename.Contains("file:///"))
			{
				vfilename = vfilename.Replace("file://", "file:///");
			}
			
			string path = vfilename.Replace("\\", "/");
			return path;
		}
		
		public static string GetFileNameNoExt(string instr)
		{
			int pos = instr.LastIndexOf(".");
			return instr.Substring(0, pos);
		}
		
		public static void EnableObject(Transform pf, bool v)
		{
			pf.gameObject.SetActive(v);
			foreach(Transform tc in pf)
			{
				EnableObject(tc, v);
			}
		}
		
		public static void SetShaderFloatValue(Transform ts, string v, float _value)
		{
			if(ts == null)
				return;
			
			if(ts.gameObject.renderer != null)
			{
				foreach(Material mt in ts.gameObject.renderer.materials)
				{
					mt.SetFloat(v, _value);
				}
			}
			
			foreach(Transform child in ts)
			{
				SetShaderFloatValue(child, v, _value);
			}
		}
		
		public static Vector3 NormalizeGridPos(Vector3 pos)
		{
			pos.x = (int)pos.x;
			pos.z = (int)pos.z;
			pos += new Vector3(0.5f, 0, 0.5f);
			
			return pos;
		}
		
		public static void SetShader(Transform ts, Shader sd)
		{
			if(ts == null)
				return;
			
			if(ts.gameObject.renderer != null)
			{
				foreach(Material mt in ts.gameObject.renderer.materials)
				{
					mt.shader = sd;
				}
			}
			
			foreach(Transform child in ts)
			{
				SetShader(child, sd);
			}
		}
		
		public static void SetLayer(Transform ts, LayerMask layer)
		{
			if(ts == null)
				return;
			
			ts.gameObject.layer = layer;
			
			foreach(Transform child in ts)
			{
				SetLayer(child, layer);
			}
		}
		
		public static void SetShaderTexture(Transform ts, string v, Texture _value)
		{
			if(ts == null)
				return;
			
			if(ts.gameObject.renderer != null)
			{
				foreach(Material mt in ts.gameObject.renderer.materials)
				{
					mt.SetTexture(v, _value);
				}
			}
			
			foreach(Transform child in ts)
			{
				SetShaderTexture(child, v, _value);
			}
		}
		
		public static void SetShaderColor(Transform ts, string v, Color _value)
		{
			if(ts == null)
				return;
			
			if(ts.gameObject.renderer != null)
			{
				foreach(Material mt in ts.gameObject.renderer.materials)
				{
					mt.SetColor(v, _value);
				}
			}
			
			foreach(Transform child in ts)
			{
				SetShaderColor(child, v, _value);
			}
		}
		
		public static void TEnableMeshCollider(Transform ts, bool tv)
		{
			if(ts.collider != null)
				ts.collider.enabled = tv;
			
			foreach(Transform t in ts)
			{
				TEnableMeshCollider(t, tv);
			}
		}
		
		public static Transform GetChildByName(Transform ts, string name)
		{
			if(ts == null)
				return null;
			
			if(ts.name.ToLower() == name.ToLower())
				return ts;
			
			Transform ret;
			foreach(Transform tc in ts)
			{
				ret = GetChildByName(tc, name);
				if(ret != null)
				{
					return ret;
				}
			}
			
			return null;
		}
		
		public static void GetChildByName(Transform ts, string name, ref List<Transform> list)
		{
			if(ts == null)
				return;
			
			if(ts.name == name)
				list.Add(ts);
			
			foreach(Transform tc in ts)
			{
				GetChildByName(tc, name, ref list);
			}
		}
		
		public static Transform[] GetAllChildByName(Transform ts, string name)
		{
			List<Transform> list = new List<Transform>();
			GetChildByName(ts, name, ref list);
			return list.ToArray();
		}
		
		public delegate float GetSceneRefHeightCallback(LayerMask _mask, out Transform ts, Vector3 pos, float refhei);
		public static GetSceneRefHeightCallback pcallbackgetrefhei = null;
		
		public delegate float GetSceneHeightCallback(float x, float z, LayerMask _mask, out Transform ts);
		public static GetSceneHeightCallback pcallbackgethei = null;
		
		public static float GetAbsoluteTerrHei(float x, float z, LayerMask _mask, out Transform ts)
		{
			if(pcallbackgethei != null)
				return pcallbackgethei(x, z, _mask, out ts);
			ts = null;
			return 0;
		}
		
		public static float GetAbsoluteTerrHei(float x, float z)
		{
			LayerMask _mask = LayerMask.NameToLayer("ground");
			Transform ts = null;
			if(pcallbackgethei != null)
				return pcallbackgethei(x, z, _mask, out ts);
			return 0;
		}
		
		public static float GetRefTerrHei(Vector3 curpos, float _refhei)
		{
			LayerMask _mask = LayerMask.NameToLayer("ground");
			Transform ts = null;
			if(pcallbackgetrefhei != null)
				return pcallbackgetrefhei(_mask, out ts, curpos, _refhei);
			return 0;
		}
		
		public static float GetRefTerrHei(Vector3 curpos, float _refhei, LayerMask _mask, out Transform ts)
		{
			if(pcallbackgetrefhei != null)
				return pcallbackgetrefhei(_mask, out ts, curpos, _refhei);
			ts = null;
			return 0;
		}
		
		public static void MakeStroke(Rect position, string txt, Color txtColor, Color outlineColor, int outlineWidth)
		{
			position.y -= outlineWidth;
			GUI.color = outlineColor;
			GUI.Label(position, txt);
			position.y += outlineWidth * 2;
			GUI.Label(position, txt);
			position.y -= outlineWidth;
			position.x -= outlineWidth;
			GUI.Label(position, txt);
			position.x += outlineWidth * 2;
			GUI.Label(position, txt);
			position.x -= outlineWidth;
			GUI.color = txtColor;
			GUI.Label(position, txt);
		}
		
		public static byte GetByteFromString(string str)
		{
			byte val = 0;
			if(byte.TryParse(str, out val))
			{
				return val;
			}
			return 0;
		}
		
		public static float GetFloatFromString(string str)
		{
			float val = 0;
			if(float.TryParse(str, out val))
			{
				return val;
			}
			return 0;
		}
	}

}
	